package control;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.HolidayDTO;
import business.HolidayManager;

/**
 * Servlet implementation class CheckHolidayServlet
 */
@WebServlet("/CheckHoliday")
public class CheckHolidayServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public CheckHolidayServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		HolidayManager hManager =new HolidayManager();
		ArrayList<HolidayDTO> holidayList = hManager.findAllHoliday();
		
		String dateString = request.getParameter("dateString");
		String period = request.getParameter("period");
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
		
		System.out.println("Holiday list size: "+holidayList.size());
		
		boolean match=false;
		for(HolidayDTO holiday : holidayList){
			String holidayString = sf.format(holiday.getHolidayDate());
			System.out.println("date string: "+dateString + " holidayString: "+ holidayString);
			if(holidayString.equals(dateString)){
				match=true;
			}
		}
		response.setContentType("text/html");
		response.getWriter().print(match);
	}

}
